package fun.tan90.easy.binlog.manager;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.JdbcTypeMapping;
import com.zaxxer.hikari.HikariDataSource;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author chj
 */
public class Codegen {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://121.36.96.79:3316/easy_binlog?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        GlobalConfig globalConfig = new GlobalConfig();

        //设置注解生成配置
        globalConfig.setAuthor("陈浩杰");

        //设置生成文件目录和根包
        globalConfig.setSourceDir("c://flex");
        globalConfig.setMapperXmlPath("c://flex//mapper");
        globalConfig.setBasePackage("fun.tan90.easy.binlog");

        //设置表前缀和只生成哪些表
        globalConfig.setGenerateTable("binlog_worker_log");
        globalConfig.setLogicDeleteColumn("is_deleted");
        globalConfig.setVersionColumn("version");

        //配置生成 entity
        globalConfig.setEntityGenerateEnable(true);
        globalConfig.setEntityWithLombok(true);
        globalConfig.setEntitySuperClass(null);

        //配置生成 mapper
        globalConfig.setMapperGenerateEnable(true);
        //配置生成 service
        globalConfig.setServiceGenerateEnable(true);
        //配置生成 serviceImpl
        globalConfig.setServiceImplGenerateEnable(true);
        //配置生成 controller
        globalConfig.setControllerGenerateEnable(true);
        //配置生成 tableDef
        globalConfig.setTableDefGenerateEnable(true);
        //配置生成 mapperXml
        globalConfig.setMapperXmlGenerateEnable(true);

        // 租户ID
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setColumnName("tenant_id");
        columnConfig.setTenantId(true);
        globalConfig.setColumnConfig(columnConfig);

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        JdbcTypeMapping.registerMapping(LocalDateTime.class, Date.class);
        //开始生成代码
        generator.generate();

        System.out.println("success");
    }
}